﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Finance
{
    public partial class RevenueMachineAccountConfirmation : NavigationPageBase
    {
        public Guid Id
        {
            get { return Parameters.GetGuid("Id"); }
        }

        public string RevenueMachineAccountStatus
        {
            get { return Parameters.GetString("Status"); }
        }

        protected IList<Guid> Ids
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.fieldIds.Value))
                    return new List<Guid>();
                return this.fieldIds.Value.Split(new char[] { ',' }).Select(id => id.ToGuid()).ToList();

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                CancelButton = btnCancel;

            }
        }

        protected void btnReLoadIds_Click(object sender, EventArgs e)
        {
            LoadInitialData();
            btnReLoadIds.Enabled = false;
        }


        private void LoadInitialData()
        {
            var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
            var revenueMachineAccount = service.GetRevenueMachineAccount(Id, true);

            FormPanelStatementOfAccount.DataSource = revenueMachineAccount;
            FormPanelStatementOfAccount.DataBind();

            this.txtConfirmStatementofAccountDate.Text = DateTime.Now.ToString("yyyy-MM-dd");


            var orderService = ServiceContainer.GetService<IIssuingOrderService>();
            var order = orderService.GetSimpleIssuingOrder(revenueMachineAccount.IssuingOrderId.Value);

            this.txtReceivingOrderDate.Text = order.ReceivingOrderDate.ToString("yyyy-MM-dd");
            txtAcutalShippingDate.Text = (order.ActualShippingDate ?? DateTime.Now).ToString("yyyy-MM-dd");

            GridViewOrderDelivery.DataSource =
                orderService.GetIssuingOrderDeliveryByOrderId(revenueMachineAccount.IssuingOrderId.Value);
            GridViewOrderDelivery.DataBind();

            this.lblTransporationPlanCode.Text = order.TransportationPlanCode;

            if (order.OrderVolumn.HasValue)
                txtOrdeVolumn.Text = order.OrderVolumn.Value.ToString("#,###.00");
            if (order.ActualOrderVolumn.HasValue)
                txtActualOrdeVolumn.Text = order.ActualOrderVolumn.Value.ToString("#,###.00");
            if (order.OrderWeight.HasValue)
                txtOrderWeight.Text = order.OrderWeight.Value.ToString("#,###.00");
            if (order.ActualOrderWeight.HasValue)
                txtActualOrderWeight.Text = order.ActualOrderWeight.Value.ToString("#,###.00");
            if (order.OrderQuantity.HasValue)
                txtOrderQuantity.Text = order.OrderQuantity.Value.ToString("#,###.00");
            if (order.ActualOrderQuantity.HasValue)
                txtActualOrderQuantity.Text = order.ActualOrderQuantity.Value.ToString("#,###.00");

            if (!IsEditable(revenueMachineAccount))
            {
                this.chbIsStatementOfAccount.Enabled =
                    txtConfirmStatementofAccountDate.Enabled = txtUnConfirmStatementofAccountComments.Enabled = false;
            }
        }

        private bool IsEditable(RevenueMachineAccountData revenueMachineAccount)
        {
            if (IsManager()) return true;
            return !(revenueMachineAccount.IsAccountsReceivable || revenueMachineAccount.IsInvoiced);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var service = ServiceContainer.GetService<IRevenueMachineAccountService>();
            var revenueMachineAccounts = new List<RevenueMachineAccountData>();
            foreach (var fieldId in Ids)
            {

                var revenueMachineAccountData = ExtractIssuingOrder(service.GetRevenueMachineAccount(fieldId, true));
                if (!revenueMachineAccountData.IsStatementofAccountCompleted && (revenueMachineAccountData.IsAccountsReceivable || revenueMachineAccountData.IsInvoiced))
                {
                    Alert("该订单已经开票或收款，不可以取消对账。请先删除开票或收款信息！");
                    return;
                }
                revenueMachineAccounts.Add(revenueMachineAccountData);

            }
            var result = service.BatchModifyRevenueMachineAccounts(revenueMachineAccounts);
            if (result)
            {
                Success("确认成功！");
                this.CloseWindow(true);
            }
        }

        private RevenueMachineAccountData ExtractIssuingOrder(RevenueMachineAccountData machineAccountData)
        {
            DateTime confirmDate = DateTime.MinValue;

            if (RevenueMachineAccountStatus == SystemConstant.StatementOfAccount)
            {
                DateTime.TryParse(txtConfirmStatementofAccountDate.Text, out confirmDate);
                machineAccountData.IsStatementofAccountCompleted = chbIsStatementOfAccount.Checked;
                if (confirmDate > DateTime.MinValue)
                {
                    machineAccountData.ConfirmStatementofAccountDate = confirmDate;
                }
                machineAccountData.UnConfirmStatementofAccountComments = txtUnConfirmStatementofAccountComments.Text;

            }


            machineAccountData.LastUpdatedBy = CurrentApplicationContext.UserId;
            machineAccountData.LastUpdatedDate = DateTime.Now;

            return machineAccountData;
        }
    }
}